﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Rocket.Core;
using Microsoft.Practices.EnterpriseLibrary.Validation;

namespace Rocket.Windows.Framework
{
    /// <summary>
    /// The BaseData is a Base data container control and BaseData control derived from BaseControl. 
    /// BaseControl derived from User Control. 
    /// 
    /// Every Data controls derived from this BaseData class.
    /// When create Child Data control you can derived it from this BaseData class and it will
    /// request Bussiness Object View for map with Child data . 
    /// MapIt() method use for map Bussiness object view with binded view.
    ///    
    ///<example>
    ///public partial class LorryCodeData : BaseData<LorryCodeView>
    ///{
    ///    public LorryCodeData()
    ///    {
    ///        InitializeComponent();
    ///    }
    ///</example>
    /// </summary>
    /// <typeparam name="V"> Bussiness Object View </typeparam>
    public class BaseDataEditor : RocketBaseData, IFormDataEditor
    {
        private ErrorProvider errorProviderEdit;
        private System.ComponentModel.IContainer components;
        private Validator validator;
        private ValidationResults results = new ValidationResults();
        public BaseDataEditor()
        { }

        public void ValidateInput<V>(V bindedView)
            where V : class, new()
        {
            validator = ValidationFactory.CreateValidator<V>();
            foreach (Control c in this.Controls)
            {
                if (c.GetType() == typeof(TextBox))
                {
                    validator.Validate(bindedView, results);
                    results.FirstOrDefault();
                    Console.Write("Is the test address valid? {0}", results.IsValid);
                    //Stop the console screen from disappearing immediately.            
                    Console.ReadKey();
                }
            }
        }

        public virtual V MapIt<V>(int Id, V bindedView)
        where V : BaseObject { return default(V); }

        public string GridViewGroupBoxName { get; set; }
        public string ManagerGroupBoxName { get; set; }
        public string GroupBoxName { get; set; }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.errorProviderEdit = new System.Windows.Forms.ErrorProvider(this.components);
            ((System.ComponentModel.ISupportInitialize)(this.errorProviderEdit)).BeginInit();
            this.SuspendLayout();
            // 
            // errorProviderEdit
            // 
            this.errorProviderEdit.ContainerControl = this;
            // 
            // BaseDataEditor
            // 
            this.Name = "BaseDataEditor";
            ((System.ComponentModel.ISupportInitialize)(this.errorProviderEdit)).EndInit();
            this.ResumeLayout(false);

        }
    }
}
